<?php

namespace app\admin\model\coupons;

use think\Model;


class Order extends Model
{

    

    

    // 表名
    protected $name = 'coupons_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'finishtime_text',
        'statusdata_text'
    ];
    
    public function couponclinic()
    {
        return $this->belongsTo('app\admin\model\coupons\Clinic', 'coupon_clinic_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function couponcategory()
    {
        return $this->belongsTo('app\admin\model\coupons\Category', 'coupon_category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

     public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function getStatusdataList()
    {
        return ['0' => __('Statusdata 0'), '1' => __('Statusdata 1'), '2' => __('Statusdata 2')];
    }


    public function getFinishtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['finishtime']) ? $data['finishtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusdataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['statusdata']) ? $data['statusdata'] : '');
        $list = $this->getStatusdataList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setFinishtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
